import {setStorage,getStorage,removeStorage} from './util'
import {get,post} from '../../http'
import { message } from 'antd';

const LOGIN = 'LOGIN';
const SMSLOGIN = 'SMSLOGIN'
const FORGET = 'FORGET'
const SMS = 'SMS'
const WXLOGIN = "WXLOGIN"
const WXLOGIN1 = 'WXLOGIN1'
const WXBIND = 'WXBIND'
const ERROR = 'ERROR'
const initState={
	res:false,
	forgetState:false,
	binddata:false,
	code:'',
	wx_bind:'N',
	bindState:false
}

// reducer1
export function userLogin(state=initState, action){
	switch(action.type){
		case LOGIN:
			return Object.assign({},state,{res:action.res,wx_bind:action.wx_bind})
		case SMSLOGIN:
			return Object.assign({},state,{res:action.res,wx_bind:action.wx_bind})
		case FORGET:
			return Object.assign({},state,{forgetState:action.res,res:false})
		case SMS:
			return Object.assign({},state,{res:false})
		case WXLOGIN:
			return Object.assign({},state,{code:'',res:action.data,wx_bind:action.wx_bind})
		case WXLOGIN1:
			return Object.assign({},state,{binddata:action.data})
		case WXBIND:
			return Object.assign({},state,{bindState:action.data})
		case ERROR:
			return Object.assign({},state,{error:action.msg})
		default:
			return state
	}
} 
function wxLoginSuccess(data,wx_bind){
    return {
		type:WXLOGIN,
		data:data,wx_bind:wx_bind
	}
}
function wxBindSuccess(data){
    return {
		type:WXBIND,
		data:data
	}
}
function wxLoginSuccess1(data){
	if(!data){
		message.warning("微信登陆失败")
	}
    return {
		type:WXLOGIN1,
		data:data
	}
}
function loginMsg(msg) {
	message.warning(msg);
	return {type:ERROR,msg:msg}
}
function loginSuccess(res,wx_bind){
	return {type:LOGIN,res:res,wx_bind:wx_bind}
}
function smsLOGINSuccess(res,wx_bind){
	return {type:SMSLOGIN,res:res,wx_bind:wx_bind}
}
function forgetSuccess(res){
	return {type:FORGET,res:res}
}
function smsSuccess(res){
	return {type:SMS,res:res}
}

export function login(iPhone,password){
	if(!iPhone){
		return loginMsg("用户名不能为空！")
	}
	if(!password){
		return loginMsg("密码不能为空！")
	}
	let data = {account:iPhone,password:password}
	return dispatch=>{
		post('/loginP.do',data)
			.then(res=>{
				if(res.success){
					let data = {}
					data.token = res.token;
					let time  = new Date().toLocaleDateString();
					time = time.replace(/[/]/g,'');
					data.time = time;
					if(!getStorage('user') ){
						setStorage('user',JSON.stringify(data))
					}else{
						removeStorage('user')
						setStorage('user',JSON.stringify(data))
					}
					dispatch(loginSuccess(res.success,res.wx_bind));
				}
					
			})
	}
	
}

export function WXloginUrl(account,password,phoneNum,verification,flag){
	let data = {account:account,password:password,phoneNum:phoneNum,verification:verification,flag}
	return dispatch=>{
		post('/wxLoginBind.do',data)
		.then(res=>{
			data.token = res.token;
			let time  = new Date().toLocaleDateString(); 
			time = time.replace(/[/]/g,'');
			data.time = time;
			if(!getStorage('user') ){
				setStorage('user',JSON.stringify(data))
			}else{
				removeStorage('user')
				setStorage('user',JSON.stringify(data))
			} 
			dispatch(wxLoginSuccess(res.success,res.wx_bind));
		})
	}
	
}

export function WXloginUrl1(code){
	if(code!==undefined || code!== ''){
		let data = {code:code}
		return dispatch=>{
			post('/wxLoginCallBack.do',data)
				.then(res=>{
					console.log(res)
					dispatch(wxLoginSuccess1(res.state));
				})		
		}
	}
}

export function wxBindState(code){
	if(code!==undefined || code!== ''){
		let data = {code:code}
		return dispatch=>{
			post('/user/wxBindCallback.do',data)
				.then(res=>{
					console.log(res)
					dispatch(wxBindSuccess(res.state));
				})		
		}
	}
}

export function forgets(phoneNum,verification,password){
	let data = {phoneNum:phoneNum,verification:verification,password:password}
	return dispatch=>{
		post('/forgetPassword.do',data)
			.then(res=>{
				console.log(res)
				dispatch(forgetSuccess(res.state));
			})		
	}
	
}
export function SMSlogin(phoneNum,verification){
	if(!phoneNum){
		return loginMsg("请输入手机号！")
	}
	if(!verification){
		return loginMsg("请输入验证码！")
	}
	let data = {phoneNum:phoneNum,verification:verification}
	return dispatch=>{
		post('/loginV.do',data)
			.then(res=>{
				if(res.success){
					let data = {}
					data.token = res.token;
					let time  = new Date().toLocaleDateString(); 
					time = time.replace(/[/]/g,'');
					data.time = time;
					if(!getStorage('user') ){
						setStorage('user',JSON.stringify(data))
					}else{
						removeStorage('user')
						setStorage('user',JSON.stringify(data))
					} 
					dispatch(smsLOGINSuccess(res.success,res.wx_bind));
				}
					
			})		
	}
	
}
export function smsList(phoneNum){
	if(!phoneNum){
		return loginMsg("请输入手机号！")
	}
	let data = {phoneNum:phoneNum}
	return dispatch=>{
		post('/sendVerification.do',data)
			.then(res=>{
					dispatch(smsSuccess(res.success));
			})		
	}
	
}

